package cn.easier.activity.console.repository;

import cn.easier.activity.console.entity.ActivityMessages;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

/**
 * 活动信息 Repository
 */
@Repository
public interface ActivityMessageRepository extends JpaRepository<ActivityMessages, Integer>, JpaSpecificationExecutor<ActivityMessages> {

    @Query(value = "select a.* from activity_message a where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) and " +
            " ((:messageTimeStart is null or :messageTimeStart = '') or a.message_time between :messageTimeStart and :messageTimeEnd) and a.`is_Del` =0 order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<ActivityMessages> findAllBy(@Param("activityId") Integer activityId, @Param("messageTimeStart") String messageTimeStart, @Param("messageTimeEnd") String messageTimeEnd, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from activity_message a where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) and " +
            " ((:messageTimeStart is null or :messageTimeStart = '') or a.message_time between :messageTimeStart and :messageTimeEnd) and a.`is_Del` =0 order by a.id desc ", nativeQuery = true)
    long findAllByCount(@Param("activityId") Integer activityId, @Param("messageTimeStart") String messageTimeStart, @Param("messageTimeEnd") String messageTimeEnd);

    @Query(value = "select a.* from activity_message a where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) and " +
            " ((:messageTimeStart is null or :messageTimeStart = '') or a.message_time between :messageTimeStart and :messageTimeEnd) and a.`is_Del` =0  and a.activity_id in :idList order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<ActivityMessages> findAllByUser(@Param("activityId") Integer activityId, @Param("idList") List<Integer> idList ,@Param("messageTimeStart") String messageTimeStart, @Param("messageTimeEnd") String messageTimeEnd, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from activity_message a where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) and " +
            " ((:messageTimeStart is null or :messageTimeStart = '') or a.message_time between :messageTimeStart and :messageTimeEnd) and a.`is_Del` =0  and a.activity_id in :idList  order by a.id desc ", nativeQuery = true)
    long findAllByCountAndUser(@Param("activityId") Integer activityId, @Param("idList") List<Integer> idList, @Param("messageTimeStart") String messageTimeStart, @Param("messageTimeEnd") String messageTimeEnd);

    Optional<ActivityMessages> findById(Long id);

    @Query("select count(a.id) from ActivityMessages a where a.userPhoneNum = :userPhoneNum")
    Long findByPhoneNum(@Param("userPhoneNum") Long userPhoneNum);

    @Query("select count(a.id) from ActivityMessages a where a.activityId = :activityId")
    Long countByActivityId(@Param("activityId") String activityId);

}
